/*
 * Copyright (C) 2016 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef vendor_ecarx_xma_gnss_V1_0_GnssPositioning_H_
#define vendor_ecarx_xma_gnss_V1_0_GnssPositioning_H_

#include <map>
#include <thread>

#include <hidl/HidlSupport.h>
#include <hidl/Status.h>
#include <netinet/in.h>
#include <vendor/ecarx/xma/gnss/1.0/IGnssPositioning.h>

namespace vendor {
namespace ecarx {
namespace xma {
namespace gnss {
namespace V1_0 {
namespace implementation {

using ::android::sp;
using ::android::wp;
using ::android::hardware::Return;
using ::android::hardware::Void;
using ::android::hardware::hidl_death_recipient;
using ::vendor::ecarx::xma::gnss::V1_0::IGnssPositioning;
using ::vendor::ecarx::xma::gnss::V1_0::IGnssPositioningCallback;
using ClientId = uint64_t;

/*
 * Extended interface for GnssPositioning support. Also contains wrapper methods to allow
 * methods from IGnssPositioningCallback interface to be passed into the conventional
 * implementation of the GNSS HAL.
 */
class GnssPositioning : public IGnssPositioning {
   public:
    GnssPositioning();
    ~GnssPositioning();

    /**
     * Subscribes to TCAM positioning service events.
     *
     * @param callback - listener This client must be called on appropriate event.
     */
    Return<StatusCode> subscribe(const sp<IGnssPositioningCallback>& callback) override;

    /**
     * Unsubscribes from TCAM positioning service events.
     *
     * @param callback - listener This client must be called on appropriate event.
     */
    Return<StatusCode> unsubscribe(const sp<IGnssPositioningCallback>& callback) override;

    ClientId getClientId(const sp<IGnssPositioningCallback>& callback);

    static void onPOIInfoNotification(const char* poiName, int64_t poiLongitude,
                                      int64_t poiLatitude, const char* street,
                                      const char* houseNumber, const char* city, const char* region,
                                      const char* postcode, const char* country);

   private:
    static std::mutex sMutex;
    static std::map<ClientId, sp<IGnssPositioningCallback>> sCallbacks;

    class HidlDeathRecipient : public hidl_death_recipient {
      public:
        HidlDeathRecipient(const sp<GnssPositioning> host) : mGnssPositioning(host) {
        }

        virtual void serviceDied(uint64_t cookie,
                const wp<::android::hidl::base::V1_0::IBase>& /*who*/) {
            mGnssPositioning->handleHidlDeath(cookie);
        }
      private:
        sp<GnssPositioning> mGnssPositioning;
    };

    sp<HidlDeathRecipient> mDeathRecipient;

    void handleHidlDeath(uint64_t cookie);
};

}  // namespace implementation
}  // namespace V1_0
}  // namespace gnss
}  // namespace xma
}  // namespace ecarx
}  // namespace vendor

#endif  // vendor_ecarx_xma_gnss_V1_0_GnssPositioning_H_
